package com.nsapi.niceschoolapi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsapi.niceschoolapi.common.base.PageData;
import com.nsapi.niceschoolapi.common.util.Constants;
import com.nsapi.niceschoolapi.common.util.Encodes;
import com.nsapi.niceschoolapi.common.util.ResponseEntity;
import com.nsapi.niceschoolapi.entity.*;
import com.nsapi.niceschoolapi.service.PoliticsService;
import com.nsapi.niceschoolapi.service.TeacherService;
import com.nsapi.niceschoolapi.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TeacherController {

    @Resource
    private PoliticsService politicsService;

    @Resource
    private UserService userService;

    @Resource
    private TeacherService teacherService;

        //进入教师授课管理页面
    @GetMapping(value = "tchcoursemanage/tchcoursemanage")
    public String tchcoursemanage(){

        return "view/teacher/tchcoursemanage";
    }

    //分页查询教师信息
    @RequestMapping(value = "/tchcoursemanage/findalltch")
    @ResponseBody
    public PageData<TeacherDB> tchcoursemanagePageData(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                 @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                                 @RequestParam(value = "tchid",required = false)String tchid,
                                                 @RequestParam(value = "tname",required = false)String tname){
        PageData<TeacherDB> pageData=new PageData<TeacherDB>();
        PageHelper.startPage(page,limit);
        //查询分页数据
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("tchid",tchid);
        map.put("tname",tname);
        List<TeacherDB> teacherPage=teacherService.teacherList(map);
        // 封装PageInfo对象，即可完成分页查询
        PageInfo pageInfo=new PageInfo(teacherPage);
        pageData.setCount(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        return pageData;
    }

    //分页查询教师名称信息
    @RequestMapping(value = "/tchcoursemanage/findalltchbyname")
    @ResponseBody
    public PageData<TeacherDB> findalltchbynamePageData(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                  @RequestParam(value = "limit" , defaultValue = "10") Integer limit){
        PageData<TeacherDB> pageData=new PageData<TeacherDB>();
        PageHelper.startPage(page,limit);
        //查询分页数据
        Map<String,Object> map=new HashMap<String,Object>();
        List<TeacherDB> teacherPage=teacherService.teacherList(map);
        // 封装PageInfo对象，即可完成分页查询
        PageInfo pageInfo=new PageInfo(teacherPage);
        pageData.setCount(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        return pageData;
    }

    //进入录入教师页面
    @GetMapping(value ="/addTeacherPage")
    public String addTeacherPage(Model model){
        List<PoliticsTypeDB> politicsList=politicsService.politicsList();
        model.addAttribute("stupol",politicsList);
        return "view/teacher/addTeacher";
    }
    //录入教师信息
    @RequestMapping(value = "/addTeacher")
    @ResponseBody
    public TeacherDB addTeacher(TeacherDB teacherDB, String birthday, String tertime) throws ParseException {
        //将String类型装换为date类型
      SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        teacherDB.setBirthday(dateFormat.parse(birthday));
        teacherDB.setTertime(dateFormat.parse(tertime));
      //生成七个字节的随机字符串作为教师编号
        String str="";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < 7; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                str += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                str += String.valueOf(random.nextInt(10));
            }
        }
        teacherDB.setTchid(str);
        int result=teacherService.addTeacher(teacherDB);
        return teacherDB;
    }
    //添加
    @RequestMapping(value = "/Teacher/user/add")
    @ResponseBody
    public boolean add(@RequestBody User user){
        ResponseEntity responseEntity= new ResponseEntity();
        user.setCreateDate(new Date()); //创建时间
        user.setCreateId(Constants.userId);
        user.setDelFlag(false);
        //给密码加密
         Encodes.entryptPassword(user);
        //添加用户信息
       userService.saveUser(user);
        //按角色名称教师信息
        Role role=politicsService.getRoleByName("教师");
        //保存用户角色关系
        UserRole userRole=new UserRole(user.getId(),role.getId());
        userService.addUserRole(userRole);
           return true;
    }
    //进入教师信息查询
    @GetMapping(value = "/selectTeacher")
    public String selectTeacher() {
        return "view/teacher/selTeacher";
    }

        //分页查询
        @RequestMapping(value = "/selTeacher/page")
        @ResponseBody
          public PageData<TeacherDB> teacherDBPageData(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                       @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                                       @RequestParam(value = "tchid",required = false)String tchid,
                                                       @RequestParam(value = "tname",required = false)String tname){
            PageData<TeacherDB> pageData=new PageData<TeacherDB>();
            PageHelper.startPage(page,limit);
            //查询分页数据
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("tchid",tchid);
            map.put("tname",tname);
            List<TeacherDB> teacherPage=teacherService.teacherList(map);
            // 封装PageInfo对象，即可完成分页查询
            PageInfo pageInfo=new PageInfo(teacherPage);
            pageData.setCount(pageInfo.getTotal());
            pageData.setData(pageInfo.getList());
            return pageData;
        }

        //查看详细信息
        @GetMapping(value = "/selectTeaMessage")
        public String teacher(Integer id,Model model){
            TeacherDB teacher=teacherService.getTeacher(id);
            List<PoliticsTypeDB> politicsTypeDBList=politicsService.politicsList();
            model.addAttribute("tea",teacher);
            model.addAttribute("selpol",politicsTypeDBList);
            return "view/teacher/selTchMessage";
        }
        //进入修改页面
    @RequestMapping(value = "/selTeacherId")
    public String Teacher(Integer id,Model model){
        TeacherDB teacher=teacherService.getTeacher(id);
        List<PoliticsTypeDB> politicsTypeDBList=politicsService.politicsList();
        model.addAttribute("tea",teacher);
        model.addAttribute("selpol",politicsTypeDBList);
        return "view/teacher/updTeacher";
    }
    //进行修改
    @RequestMapping(value = "/updateTeacher")
    @ResponseBody
    public ResponseEntity updateTeacher(TeacherDB teacherDB){
        ResponseEntity response=new ResponseEntity();

        int reuslt=teacherService.updateTeacher(teacherDB);
        if (reuslt>0){
            response.setAny("msg","修改成功！");
        }else{
            response.setAny("msg","修改失败！");
        }
        return response;
    }
}
